package controller;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import database.Queries;

public class Student {
	
	boolean isStudent=true;
	String username;
	String password;
	String fullname;
	String phone;
	LinkedList<Course> courses=null;
	
		public Student(String username,String password,String role,String fullname,String phone, LinkedList<Course> sCourses) throws SQLException{
			this.username = username;
			this.password = password;
			this.fullname = fullname;
			this.phone = phone;
			if (role.equals("admin")){
				isStudent = false;
			}
			else {
				if(sCourses!=null)
				{
					courses = sCourses;
				}
				else
				{
					courses=new LinkedList<Course>();
				}
			}
		}
	
	
		public LinkedList<Course> getCourses(){
			return courses;
		}

		public String getUsername() {
			return username;
		}

		public String getPassword() {
			return password;
		}

		public String getFullname() {
			return fullname;
		}

		public String getPhone() {
			return phone;
		}
		
		public boolean isStudent(){
			return isStudent;
		}
		
		public boolean isRegisteredToCourse(String courseName){
			if (courses==null){
				return false;
			}
			Iterator<Course> iter = courses.iterator();
			while (iter.hasNext()){
				if (iter.next().getName().equals(courseName)){
					return true;
				}
			}
			return false;	
		}
		
		public boolean canRegisterGroup(Course course)
		{
			Set<Integer> set=new HashSet<Integer>();
			Iterator<Course> i=courses.iterator();
			while(i.hasNext())
			{
				Integer groupNum=i.next().getGroupNum();
				if(!set.contains(groupNum))
				{
					set.add(groupNum);
				}
			}
			if(set.size()==3 && !set.contains(course.getGroupNum()))
			{
				return false;
			}
			return true;
		}
		
		public boolean canRegisterToCourse(Course course)
		{
			if(course.getCapacity()==Queries.studentNumInCourse(course.getName()))
			{
				return false;
			}
			if(this.isRegisteredToCourse(course.getName()))
			{
				return false;
			}
			Iterator<Course> i=courses.iterator();
			while(i.hasNext())
			{
					if(i.next().collideWith(course))
					{
						return false;
					}
			}
			if(!this.canRegisterGroup(course))
			{
				return false;
			}
			return true;
		}
}
